package com.woniu.service.impl;

import com.woniu.entity.Attendence1;
import com.woniu.mapper.*;
import com.woniu.pojo.*;
import com.woniu.pojo.Class;
import com.woniu.service.AttendenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class AttendenceServiceImpl implements AttendenceService {
    @Autowired
    AttendenceDao attendenceDao;
   @Autowired
    StudentDao studentDao;
   @Autowired
    EmpDao empDao;
    @Override
    public void add(Attendence attendence) {
        attendenceDao.insert(attendence);
    }

    @Override
    public void deleteById(Integer attendenceId) {
        attendenceDao.deleteByPrimaryKey(attendenceId);
    }

    @Override
    public Attendence1 findOne(Integer attendenceId) {
        return attendenceDao.selectByPrimaryKey1(attendenceId);
    }

    @Override
    public void updateById(Attendence attendence) {
        System.out.println(attendence);
        attendenceDao.updateByPrimaryKey(attendence);
    }

    @Override
    public List<Attendence1> findAll() {
        return attendenceDao.selectByExample1(null);
    }

    @Override
    public void deleteByExample(List<Integer> asList) {
        AttendenceExample attendenceExample=new AttendenceExample();
        attendenceExample.createCriteria().andAttendenceIdIn(asList);
        attendenceDao.deleteByExample(attendenceExample);
    }

    @Override
    public List<Attendence> selectAll() {
        return attendenceDao.selectByExample(null);
    }

    @Override
    public Attendence selectOne(Integer attendenceId) {
        return attendenceDao.selectByPrimaryKey(attendenceId);
    }



    @Override
    public List<Attendence1> findDate(Map<String, Object> mapCondition) {

        AttendenceExample attendenceExample=new AttendenceExample();
        AttendenceExample.Criteria criteria=attendenceExample.createCriteria();
        Attendence attendence= (Attendence) mapCondition.get("attendence");
        if(mapCondition.get("start")!=null&&!mapCondition.get("start").equals("")){
            criteria.andAttendenceDateGreaterThanOrEqualTo((Date) mapCondition.get("start"));
        }
        if(mapCondition.get("end")!=null&&!mapCondition.get("end").equals("")){
            criteria.andAttendenceDateLessThanOrEqualTo((Date) mapCondition.get("end"));
        }
        if(attendence.getStudentId()!=null&&!attendence.getStudentId().equals("")){
            criteria.andStudentIdEqualTo(attendence.getStudentId());
        }
        if(attendence.getClassId()!=null&&!attendence.getClassId().equals("")){
            criteria.andClassIdEqualTo(attendence.getClassId());
        }
        if(attendence.getCourseId()!=null&&!attendence.getCourseId().equals("")){
            criteria.andCourseIdEqualTo(attendence.getCourseId());
        }
        if(attendence.getEmpId()!=null&&!attendence.getEmpId().equals("")){
            criteria.andEmpIdEqualTo(attendence.getEmpId());
        }
        if(attendence.getAttendenceDate()!=null&&!attendence.getAttendenceDate().equals("")){
            criteria.andAttendenceDateEqualTo(attendence.getAttendenceDate());
        }
        if(mapCondition.get("empIdList")!=null){
            //得到studentId集合
            List<Integer> empIdList = (List<Integer>) mapCondition.get("empIdList");

            if(empIdList.size()>0){
                criteria.andEmpIdIn(empIdList);
            }
        }
        if(mapCondition.get("studentIdList")!=null){
            //得到empId集合
            List<Integer> studentIdList = (List<Integer>) mapCondition.get("studentIdList");
            if (studentIdList.size()>0){
                criteria.andStudentIdIn(studentIdList);
            }
        }
        if(mapCondition.get("classIdList")!=null){
            //得到empId集合
            List<Integer> classIdList = (List<Integer>) mapCondition.get("classIdList");
            if (classIdList.size()>0){
                criteria.andClassIdIn(classIdList);
            }
        }
        attendenceExample.setOrderByClause("attendence_date desc,attendence_result desc");
        return attendenceDao.selectByExample1(attendenceExample);
    }

    @Override
    public String updateResultById(Integer attendenceId) {

        Attendence attendence = attendenceDao.selectByPrimaryKey(attendenceId);
        if (attendence.getAttendenceResult().equals("到")){
            attendence.setAttendenceResult("缺勤");
        }else {
            attendence.setAttendenceResult("到");
        }
        attendenceDao.updateByPrimaryKey(attendence);
        return attendence.getAttendenceResult();
    }
}
